package com.hebeiwangxiaozhang.admin.controller;

import com.hebeiwangxiaozhang.admin.pojo.CommodityParam;
import com.hebeiwangxiaozhang.admin.pojo.CommoditySku;
import com.hebeiwangxiaozhang.admin.pojo.CommoditySpuDetail;
import com.hebeiwangxiaozhang.admin.service.CommodityService;
import com.hebeiwangxiaozhang.response.CommonResponse;
import com.hebeiwangxiaozhang.response.ResponseCode;
import com.hebeiwangxiaozhang.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResponse createCommodity(@RequestBody CommodityParam commodityParam) {
        try {
            commodityService.createCommodity(commodityParam);
            return ResponseUtils.okCreatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResponse updateCommodity(@RequestBody CommodityParam commodityParam) {
        try {
            commodityService.updateCommodity(commodityParam);
            return ResponseUtils.okUpdatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/spu/detail/{id}", method = RequestMethod.GET)
    public CommonResponse getSpuDetail(@PathVariable Long id) {
        try {
            CommoditySpuDetail spuDetail = commodityService.getSpuDetailBySpuId(id);
            return ResponseUtils.okResponse(spuDetail);
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/sku/list/{spuId}", method = RequestMethod.GET)
    public CommonResponse getSkuBySpuId(@PathVariable Long spuId) {
        try {
            List<CommoditySku> skuList = commodityService.getSkuBySpuId(spuId);
            return ResponseUtils.okResponse(skuList);
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    //商品上架 - 初次的尝试
    @Deprecated
    @RequestMapping(value = "/on-sale/spu/{spuId}", method = RequestMethod.POST)
    public CommonResponse onSale(@PathVariable Long spuId) {
        commodityService.onSale(spuId);
        return ResponseUtils.okResponse(null);
    }

    //商品上架 - attrs 优化
    @Deprecated
    @RequestMapping(value = "/on-sale1/spu/{spuId}", method = RequestMethod.POST)
    public CommonResponse onSale1(@PathVariable Long spuId) {
        commodityService.onSale1(spuId);
        return ResponseUtils.okResponse(null);
    }

    @Deprecated
    @RequestMapping(value = "/on-sale2/spu/{spuId}", method = RequestMethod.POST)
    public CommonResponse onSale2(@PathVariable Long spuId) {
        commodityService.onSale2(spuId);
        return ResponseUtils.okResponse(null);
    }

    @Deprecated
    @RequestMapping(value = "/on-sale3/spu/{spuId}", method = RequestMethod.POST)
    public CommonResponse onSale3(@PathVariable Long spuId) {
        return ResponseUtils.okResponse(commodityService.onSale3(spuId));
    }

    @RequestMapping(value = "/on-sale4/spu/{spuId}", method = RequestMethod.POST)
    public CommonResponse onSale4(@PathVariable Long spuId) {
        return commodityService.onSale4(spuId);
    }
}
